package com.wqiuqiu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.wqiuqiu.common.excption.BizCodeEnume;
import com.wqiuqiu.common.to.login.AccountTo;
import com.wqiuqiu.common.to.thridservice.Oauth2To;
import com.wqiuqiu.gulimall.member.exception.UserRegException;
import com.wqiuqiu.gulimall.member.feign.CouponFeignService;
import com.wqiuqiu.gulimall.member.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wqiuqiu.gulimall.member.entity.MemberEntity;
import com.wqiuqiu.gulimall.member.service.MemberService;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.R;


/**
 * 会员
 *
 * @author wqiuqiu
 * @email 18773489795@163.com
 * @date 2023-11-29 12:11:20
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("zhangsan");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
    }

    /**
     * 会员注册
     *
     * @param vo
     * @return
     */
    @PostMapping("/memberReg")
    public R memberReg(@RequestBody RegisterVo vo) {
        try {
            memberService.memberReg(vo);
            return R.ok();
        } catch (UserRegException e) {
            return R.error(BizCodeEnume.USER_REGISTER_EXCEPTION.getCode(), e.getMessage());
        }
    }

    /**
     * gulimall帐号登录
     *
     * @param to
     * @return
     */
    @PostMapping("/gulimall/login")
    public R gulimallLogin(@RequestBody AccountTo to) {
        return memberService.gulimallLogin(to);
    }

    /**
     * 第三方登录
     *
     * @param oauth2To
     * @return
     */
    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody Oauth2To oauth2To) throws InterruptedException {
//        Thread.sleep(30000);
        MemberEntity memberEntity = memberService.oauth2Login(oauth2To);
        return R.ok().setData(memberEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("data", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
